package cc.vv.btpartyjob.activity

import android.content.Intent
import android.os.Message
import android.text.TextUtils
import android.view.View
import android.view.WindowManager
import cc.vv.baselibrary.bean.base.BaseResponseObj
import cc.vv.baselibrary.global.UserInfoSharePreKey
import cc.vv.baselibrary.http.BaseHttpRequest
import cc.vv.baselibrary.util.LKPrefUtil
import cc.vv.baselibrary.util.LKTimeUtil
import cc.vv.baselibrary.util.LKToastUtil
import cc.vv.baselibrary.util.UserInfoManageUtil
import cc.vv.baselibrary.util.router.RouterIntentUtil
import cc.vv.baselibrary.view.BaseTopBarView
import cc.vv.btpartyjob.R
import cc.vv.btpartyjob.delegate.AddPartyRegisterActivityDelegate
import cc.vv.btpartyjob.jnimodule.activity.JLOrganizationActivity
import cc.vv.btpartyjob.jnimodule.activity.OrganizationActivity
import cc.vv.btpartyjob.jnimodule.globle.IntentJniKey
import cc.vv.btpartyjob.jnimodule.server.ResourceChooseServer
import cc.vv.btpartyjob.module.bean.AddPartyRegisterObj
import cc.vv.btpartyjob.module.resp.AddPartyRegisterResponseObj
import cc.vv.btpartyjob.partymanager.delegate.ManagerBaseBinder
import cc.vv.lkbasecomponent.http.lib.setting.Settings
import cc.vv.lklibrary.http.base.okhttpfactory.plugins.httpplugin.MediaTypeWrap
import cc.vv.mvp.activity.BaseActivityMVP
import cc.vv.mvp.binder.BaseDataBinder
import com.dawning.utiming.login.api.LoginApi

/**
 * 新增党员报到
 * Created by Chen.qi on 2018/10/27.
 */
class AddPartyRegisterActivity : BaseActivityMVP<AddPartyRegisterActivityDelegate>() {

    /**
     * 街道id
     */
    private var jieDaoId: String? = null
    /**
     * 社区id
     */
    private var sheQuId: String? = null
    /**
     * 网格id
     */
    private var wangGeId: String? = null
//    /**
//     * 单位id
//     */
//    private var danWeiId: String? = null

    /**
     * 是否成为志愿者
     */
    private var volunteer: Int = 0

    /**
     * 入职时间
     */
    private var longEndTime: Long = 0

    private var street: Boolean = false
    private var shequ: Boolean = false
    private var wangge: Boolean = false

    override fun initData() {
        window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN)
        viewDelegate?.setTopBarTitle("党员报到")
        viewDelegate?.selectedVolunteer(true)
        viewDelegate?.setName(LKPrefUtil.getString(UserInfoSharePreKey.USER_NAME, ""))
    }

    override fun bindEvenListener() {
        viewDelegate?.setTitleBackBtnOnClick(object : BaseTopBarView.EventInterface {
            //返回
            override fun leftOnClick() {
                finish()
            }

            override fun leftCloseClick() {}
            override fun rightOnClick() {
                //退出登录
                exitApp()
                LKPrefUtil.clearSP(UserInfoSharePreKey.TOKEN, UserInfoSharePreKey.LOGIN_INFO,
                        UserInfoSharePreKey.TEL_PASSWORD)
                LKPrefUtil.clearSP(UserInfoSharePreKey.YUE_PAIMING_TOP, UserInfoSharePreKey.YUE_PAIMING_BOTTOM,
                        UserInfoSharePreKey.TOTAL_PAIMING_BOTTOM, UserInfoSharePreKey.TOTAL_PAIMING_TOP)
                LKPrefUtil.clearSP(UserInfoSharePreKey.USERINFO_DATA, UserInfoSharePreKey.GE_REN_DATA)
                RouterIntentUtil.getInstance().intentLoginActivity(mContext)
            }
        })

        viewDelegate?.setOnClickListener(View.OnClickListener {
            //街道选择
            val intent = Intent(this@AddPartyRegisterActivity, OrganizationActivity::class.java)
            intent.putExtra(IntentJniKey.INTENT_KEY_ORG_TYPE, 1)
            intent.putExtra(IntentJniKey.INTENT_KEY_ORG_CHOOSE, 1)
            startActivityForResult(intent, 100)
        }, R.id.ll_apr_registerStreet)

        viewDelegate?.setOnClickListener(View.OnClickListener {
            //社区选择
            if (!street) {
                LKToastUtil.showToastShort("请先选择街道")
                return@OnClickListener
            }
            val intent = Intent(this@AddPartyRegisterActivity, JLOrganizationActivity::class.java)
            intent.putExtra(IntentJniKey.INTENT_KEY_SEARCH_CHILD_LIST_ID, jieDaoId)
            intent.putExtra(IntentJniKey.INTENT_KEY_SEARCH_CHILD_LIST_TYPE, "2")
            startActivityForResult(intent, 101)
        }, R.id.ll_apr_registerCommunity)

        viewDelegate?.setOnClickListener(View.OnClickListener {
            //网格选择
            if (!shequ) {
                LKToastUtil.showToastShort("请先选择社区")
                return@OnClickListener
            }
            val intent = Intent(this@AddPartyRegisterActivity, JLOrganizationActivity::class.java)
            intent.putExtra(IntentJniKey.INTENT_KEY_SEARCH_CHILD_LIST_ID, sheQuId)
            intent.putExtra(IntentJniKey.INTENT_KEY_SEARCH_CHILD_LIST_TYPE, "3")
            startActivityForResult(intent, 102)
        }, R.id.ll_apr_wangGe)

//        viewDelegate?.setOnClickListener(View.OnClickListener {
//            //单位选择
//            val intent = Intent(this@AddPartyRegisterActivity, OrganizationActivity::class.java)
//            intent.putExtra(IntentJniKey.INTENT_KEY_ORG_TYPE, 4)
//            intent.putExtra(IntentJniKey.INTENT_KEY_ORG_CHOOSE, 4)
//            startActivityForResult(intent, 103)
//        }, R.id.ll_apr_placeTheUnit)

        viewDelegate?.setOnClickListener(View.OnClickListener {
            //工作时间
            ResourceChooseServer.chooseData(this, handler, 10, 2)
        }, R.id.ll_apr_workTime)


        viewDelegate?.setOnClickListener(View.OnClickListener {
            //是志愿者
            volunteer = 0
            viewDelegate?.selectedVolunteer(true)
        }, R.id.ll_addVo_volunteerYes)

        viewDelegate?.setOnClickListener(View.OnClickListener {
            //非志愿者
            volunteer = 1
            viewDelegate?.selectedVolunteer(false)
        }, R.id.ll_addVo_volunteerNo)


        viewDelegate?.setOnClickListener(View.OnClickListener {
            //提交
            if (TextUtils.isEmpty(jieDaoId) || TextUtils.isEmpty(sheQuId)
                    || TextUtils.isEmpty(wangGeId)) {
                LKToastUtil.showToastShort("请选择区域")
                return@OnClickListener
            }


            val phone = viewDelegate?.getPhone()
            val zhiYeTeDian = viewDelegate?.getZhiYeTeDian()
            val aiHaoTeChang = viewDelegate?.getAiHaoTeChang()
            val geRenChengNuo = viewDelegate?.getGeRenChengNuo()

            if (TextUtils.isEmpty(phone)) {
                LKToastUtil.showToastShort("请填写电话号码")
                return@OnClickListener
            }

            if (longEndTime < 1) {
                LKToastUtil.showToastShort("请选择时间")
                return@OnClickListener
            }

            val param = AddPartyRegisterObj()
            param.street = jieDaoId
            param.community = sheQuId
            param.grid = wangGeId
            param.name = LKPrefUtil.getString(UserInfoSharePreKey.USER_NAME, "")
            param.mobile = phone
            param.professionalCharacteristics = zhiYeTeDian
            param.workTime = longEndTime
            param.specialHobby = aiHaoTeChang
            param.personalCommitment = geRenChengNuo
            param.volunteer = volunteer
            param.id = UserInfoManageUtil.getUserId()

            BaseHttpRequest.postRequest(LoginApi.addPartyRegisterUrl,
                    param, AddPartyRegisterResponseObj::class.java, true,
                    Settings(MediaTypeWrap.MEDIA_TYPE_RAW_JSON))

        }, R.id.rl_apr_submit)
    }

    override fun getData(obj: BaseResponseObj<*>?) {
        super.getData(obj)
        when (obj) {
            is AddPartyRegisterResponseObj -> {
                //报到成功
                LKPrefUtil.putBoolean(UserInfoSharePreKey.PARTY_IS_REPORT, false)
                intentAppMainActivity()
            }
        }
    }

    override fun getBadCode(obj: BaseResponseObj<*>?) {
        super.getBadCode(obj)
        //报到失败
        LKToastUtil.showToastShort("网络异常")
    }

    /** 跳转应用主界面*/
    private fun intentAppMainActivity() {
        val intent = Intent(this, AppMainActivity::class.java)
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
        intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP)
        startActivity(intent)
        finish()
    }

    override fun onHandleMsg(msg: Message?) {
        super.onHandleMsg(msg)
        when (msg?.what) {
            10 -> {
                //结束时间
                val dateString: String = msg.obj as String
                longEndTime = LKTimeUtil.getInstance().getLongTime(dateString, "yyyy-MM-dd")
                viewDelegate?.setWorkTime(dateString)
            }
        }
    }

    override fun getDelegateClass(): Class<AddPartyRegisterActivityDelegate> {
        return AddPartyRegisterActivityDelegate::class.java
    }

    override fun getDataBinder(): BaseDataBinder<*, *> {
        return ManagerBaseBinder()
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (null == data) {
            return
        }

        if (requestCode == 100 && resultCode == 101) {
            //街道选择
            street = true
            jieDaoId = data.getStringExtra(IntentJniKey.INTENT_KEY_ORGAN_ID)
            val jieDaoName = data.getStringExtra(IntentJniKey.INTENT_KEY_ORGAN_NAME)
            viewDelegate?.setStreetStr(jieDaoName)

            sheQuId = ""
            wangGeId = ""
            viewDelegate?.setCommunityStr("请选择")
            viewDelegate?.setWangGeUnitStr("请选择")
            shequ = false
        } else if (requestCode == 101 && resultCode == 101) {
            //社区选择
            shequ = true
            sheQuId = data.getStringExtra(IntentJniKey.INTENT_KEY_ORGAN_ID)
            val sheQuName = data.getStringExtra(IntentJniKey.INTENT_KEY_ORGAN_NAME)
            viewDelegate?.setCommunityStr(sheQuName)
            wangGeId = ""
            viewDelegate?.setWangGeUnitStr("请选择")

        } else if (requestCode == 102 && resultCode == 101) {
            //网格选择
            wangGeId = data.getStringExtra(IntentJniKey.INTENT_KEY_ORGAN_ID)
            val wangGeName = data.getStringExtra(IntentJniKey.INTENT_KEY_ORGAN_NAME)
            viewDelegate?.setWangGeUnitStr(wangGeName)
        }
//        else if (requestCode == 103) {
//            //单位选择
//            danWeiId = data.getStringExtra(IntentJniKey.INTENT_KEY_ORGAN_ID)
//            val danWeiName = data.getStringExtra(IntentJniKey.INTENT_KEY_ORGAN_NAME)
//            viewDelegate?.setDanWeiStr(danWeiName)
//        }
    }
}